<?php
namespace app\sysadmin\controller;
use app\common\controller\Base as Base;
use think\Db;
class Common extends Base{
	protected $admin_id = 0; // 管理员ID
	protected $role_id = 0; // 角色ID
	public function __construct(){
		parent::__construct();
		
		$this->admin_id = intval(session('admin_id'));
		if($this->admin_id > 0){
			
		}
		if(!($this->admin_id > 0)){
			session(null);
			$this->redirect('login/index');
		}
		
		$layui_version = date('YmdHis').rand(0,9);
		$this->assign('version', "?v=".$layui_version);
		$this->assign('layui_version', $layui_version);
		
		$this->assign('source', $this->_public."sysadmin/");
	}
	public function _empty(){
        return $this->tips_404();
    }
	protected function tips_404(){
		header('HTTP/1.1 404 Not Found');
		header("status: 404 Not Found");
		return $this->fetch('common/404');
		exit;
	}
	protected function add_operation_log($_key=0,$_data){
		// $request = Request::instance();
		// echo "当前模块名称是" . $request->module();
		// echo "当前控制器名称是" . $request->controller();
		// echo "当前操作名称是" . $request->action();

		// echo "当前模块名称是" . request()->module();
		// echo "当前控制器名称是" . request()->controller();
		// echo "当前操作名称是" . request()->action();
		
		$_admin_id		= $this->admin_id;
		$_module		= $this->request->module();
		$_controller	= $this->request->controller();
		$_action		= $this->request->action();
		$_ip			= $this->request->ip();
		$_url			= $this->request->server('HTTP_REFERER');
		$_user_agent	= $this->request->server('HTTP_USER_AGENT');
		$_opera_date	= date('Y-m-d H:i:s');
		
		$_log_data = array();
		if(is_array($_key)){
			// 多条记录
			if($_data){
				foreach($_data as $k=>$v){
					$_log_data[] = array(
						'key'			=> $v['id'],
						'admin_id'		=> $_admin_id,
						'module'		=> $_module,
						'controller'	=> $_controller,
						'action'		=> $_action,
						'ip'			=> $_ip,
						'url'			=> $_url,
						'user_agent'	=> $_user_agent,
						'opera_date'	=> $_opera_date,
						'data_json'		=> json_encode($v)
					);
				}
			}
			if($_log_data){
				$this->add_admin_opera_log(1,$_log_data);
			}
		}else{
			// 单条记录
			$_log_data['key']			= $_key;
			$_log_data['admin_id']		= $_admin_id;
			$_log_data['module']		= $_module;
			$_log_data['controller']	= $_controller;
			$_log_data['action']		= $_action;
			$_log_data['ip']			= $_ip;
			$_log_data['url']			= $_url;
			$_log_data['user_agent']	= $_user_agent;
			$_log_data['opera_date']	= $_opera_date;
			$_log_data['data_json']		= json_encode($_data);
		}
		if($_log_data){
			$this->add_admin_opera_log(0,$_log_data);
		}
	}
	private function add_admin_opera_log($t=0,$_login_log){
	
		$_tables_name = config("database.prefix")."admin_opera_log_".date('Y');
		$_sql = "show tables like '".$_tables_name."'";
		
		$is_tables = Db::query($_sql);
		if(!$is_tables){
			// 添加数据库
			$_add_sql = "CREATE TABLE `".$_tables_name."` (
						  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号',
						  `key` int(11) NOT NULL DEFAULT '0' COMMENT '操作数据主键',
						  `admin_id` int(11) NOT NULL DEFAULT '0' COMMENT '操作人id',
						  `module` varchar(200) NOT NULL COMMENT '模块名称',
						  `controller` varchar(200) NOT NULL COMMENT '控制器名称',
						  `action` varchar(200) NOT NULL COMMENT '操作名称',
						  `ip` varchar(200) NOT NULL COMMENT '操作ip',
						  `url` varchar(400) NOT NULL COMMENT '操作网址',
						  `user_agent` text NOT NULL COMMENT '浏览器信息',
						  `data_json` longtext NOT NULL COMMENT '操作数据',
						  `opera_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '操作时间',
						  PRIMARY KEY (`id`)
						) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='管理员操作日志';";
			Db::query($_add_sql);
		}
		$rule = [
			'type' => 'year',
			'expr' => '1',
		];
		if($t == 0){
			Db::table($_tables_name)->strict(false)->insert($_login_log);
		}else{
			Db::table($_tables_name)->strict(false)->insertAll($_login_log);
		}
		// Db::table('admin_login')->partition(['login_time' => $login_time], "login_time", $rule)->insert($_login_log);
	}

}
?>